namespace CIS
{
    using CISLib;
    using System;
    using System.IO;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class LogManagePage : Page
    {
        protected Button btnExport;
        protected TextBox DayTxb;
        protected DataGrid dgLog;
        protected DropDownList EndDayDdl;
        protected DropDownList EndMonthDdl;
        protected DropDownList EndYearDdl;
        protected Image ilogManage;
        protected Label lbFrom;
        protected Label lbFromD;
        protected Label lbFromM;
        protected Label lbFromY;
        protected Label lbLevel;
        protected Label lbLevelDay;
        protected Label lbTo;
        protected Label lbToD;
        protected Label lbToM;
        protected Label lbToY;
        protected DropDownList LevelDdl;
        protected DataGrid LogDg;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;
        protected Button SearchBtn;
        protected DropDownList StartDayDdl;
        protected DropDownList StartMonthDdl;
        protected DropDownList StartYearDdl;

        private void btnExport_Click(object sender, EventArgs e)
        {
            this.ExportToExcel(this.dgLog, "attachment;filename=LogManage.xls", "application/ms-excel");
        }

        private void ExportToExcel(DataGrid dg, string name, string type)
        {
            dg.Visible = true;
            base.Response.Clear();
            base.Response.Buffer = true;
            base.Response.Charset = "GB2312";
            base.Response.AppendHeader("Content-Disposition", name);
            base.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            base.Response.ContentType = type;
            dg.EnableViewState = false;
            StringWriter oStringWriter = new StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            dg.RenderControl(oHtmlTextWriter);
            base.Response.Write(oStringWriter.ToString());
            base.Response.End();
            dg.Visible = false;
        }

        private void FillPage()
        {
            this.LogDgDataBind();
        }

        private void InitializeComponent()
        {
            this.LevelDdl.SelectedIndexChanged += new EventHandler(this.LevelDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            this.SearchBtn.Click += new EventHandler(this.SearchBtn_Click);
            this.btnExport.Click += new EventHandler(this.btnExport_Click);
            this.LogDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.LogDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.SetFromImage();
            this.LogDg.PageSize = 30;
            this.LevelDdl.Items.Clear();
            for (int i = 1; i <= 5; i++)
            {
                string tmpStr = "level" + i.ToString();
                ListItem tmpItem = new ListItem();
                tmpItem.Text = tmpStr;
                tmpItem.Value = tmpStr;
                this.LevelDdl.Items.Add(tmpItem);
            }
            this.StartYearDdl.Items.Clear();
            this.StartMonthDdl.Items.Clear();
            this.StartDayDdl.Items.Clear();
            for (int i = 0x7d5; i <= DateTime.Now.Year; i++)
            {
                ListItem YearItem = new ListItem();
                YearItem.Text = i.ToString();
                this.StartYearDdl.Items.Add(YearItem);
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.StartMonthDdl.Items.Add(MonthItem);
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.StartDayDdl.Items.Add(DateItem);
            }
            this.EndYearDdl.Items.Clear();
            this.EndMonthDdl.Items.Clear();
            this.EndDayDdl.Items.Clear();
            for (int i = 0x7d5; i <= DateTime.Now.Year; i++)
            {
                ListItem YearItem = new ListItem();
                YearItem.Text = i.ToString();
                this.EndYearDdl.Items.Add(YearItem);
                this.EndYearDdl.SelectedIndex = this.EndYearDdl.Items.Count - 1;
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.EndMonthDdl.Items.Add(MonthItem);
                this.EndMonthDdl.SelectedIndex = this.EndMonthDdl.Items.Count - 1;
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.EndDayDdl.Items.Add(DateItem);
                this.EndDayDdl.SelectedIndex = this.EndDayDdl.Items.Count - 1;
            }
            this.DayTxb.Text = ManageSystemConfig.GetSystemConfig(this.LevelDdl.SelectedItem.Text);
        }

        private void LevelDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DayTxb.Text = ManageSystemConfig.GetSystemConfig(this.LevelDdl.SelectedItem.Text);
        }

        private void LogDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.LogDg.CurrentPageIndex = e.NewPageIndex;
            this.LogDgDataBind();
        }

        private void LogDgDataBind()
        {
            try
            {
                DateTime startDateTime = new DateTime(Convert.ToInt32(this.StartYearDdl.SelectedItem.Text), Convert.ToInt32(this.StartMonthDdl.SelectedItem.Text), Convert.ToInt32(this.StartDayDdl.SelectedItem.Text));
                DateTime endDateTime = new DateTime(Convert.ToInt32(this.EndYearDdl.SelectedItem.Text), Convert.ToInt32(this.EndMonthDdl.SelectedItem.Text), Convert.ToInt32(this.EndDayDdl.SelectedItem.Text)).AddDays(1.0);
                this.LogDg.DataSource = ManageLog.GetLog(startDateTime, endDateTime);
                this.LogDg.DataBind();
                this.dgLog.DataSource = ManageLog.GetLog(startDateTime, endDateTime);
                this.dgLog.DataBind();
            }
            catch (Exception)
            {
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.LogDgDataBind();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                int tmp = Convert.ToInt32(this.DayTxb.Text.Trim());
                ManageSystemConfig.UpdateSystemConfig(this.LevelDdl.SelectedItem.Text, this.DayTxb.Text.Trim());
            }
            catch (Exception)
            {
            }
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            this.LogDgDataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbLevel.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbLevel", language, path);
            this.lbLevelDay.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbLevelDay", language, path);
            this.lbFrom.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbFrom", language, path);
            this.lbFromY.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbFromY", language, path);
            this.lbFromM.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbFromM", language, path);
            this.lbFromD.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbFromD", language, path);
            this.lbTo.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbTo", language, path);
            this.lbToY.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbToY", language, path);
            this.lbToM.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbToM", language, path);
            this.lbToD.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "lbToD", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "SaveBtn", language, path);
            this.SearchBtn.Text = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "SearchBtn", language, path);
            this.LogDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "name", language, path);
            this.LogDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "userIp", language, path);
            this.LogDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "logTime", language, path);
            this.LogDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "operationType", language, path);
            this.LogDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("LogManagePage", "customerName", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.ilogManage.ImageUrl = "images/title_LogManage.jpg";
                    break;

                case "en":
                    this.ilogManage.ImageUrl = "images/title_LogManageEn.jpg";
                    break;
            }
        }
    }
}

